package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.order.api.ProductApi;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Slf4j
@Component
public class ClearCartItemHandler extends AbstractTransHandler {

    // 删除购物车商品，调用商品服务
    @Autowired
    ProductApi productApi;


    //是否采用异步方式执行
    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        Long userId = createOrderContext.getUserId();
        List<Long> productIds = createOrderContext.getBuyProductIds();
        ClearCartItemRequest request = new ClearCartItemRequest();
        request.setUserId(userId);
        request.setProductIds(productIds);
        ClearCartItemResponse response = productApi.clearCartItemByUserID(request);
        if(!ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            throw new BizException("清空购物车失败！");
        }
        return true;
    }
}
